package com.eclipse.e0112_多线程.线程协作_喝水;

public class Test {
	public static void main(String[] args) {

		Cup cup = new Cup();
		Drink d = new Drink(cup);
		d.start();
		Waiter w = new Waiter(cup);
		w.start();
	}
}

class Cup {
	private int shui;

	int getShui() {
		return this.shui;
	}

	void heshui() {
		shui--;
		shui--;
		shui--;
		shui--;
	}

	void daoshui() {
		shui++;
		shui++;
		shui++;
		shui++;
	}
}

class Drink extends Thread {
	private Cup cup;

	Drink(Cup cup) {
		this.cup = cup;
	}

	public void run() {
		synchronized (cup) {
			while (true) {

				try {
					cup.wait();
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				System.out.println("喝水");

				cup.heshui();
				System.out.println(cup.getShui());

				cup.notify();
			}
		}
	}
}

class Waiter extends Thread {
	private Cup cup;

	Waiter(Cup cup) {
		this.cup = cup;
	}

	public void run() {
		synchronized (cup) {
			while (true) {
				if (cup.getShui() < 4) {
					System.out.println("倒水");

					cup.daoshui();
					System.out.println(cup.getShui());
					cup.notify();
					System.out.println("唤醒，自己睡觉");
//					try {
//						Thread.currentThread().sleep(3000);
//					} catch (InterruptedException e1) {
//						// TODO Auto-generated catch block
//						e1.printStackTrace();
//					}
					try {
						cup.wait();
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			}
		}
	}
}